// 数据存储对象
const data = {
    activeTab: 'reviews',
    reviews: [{
            id: 1,
            user: {
                name: '美食爱好者'
            },
            date: '2025-06-10',
            content: '这道菜简直太美味了！口感丰富，味道层次分明，食材新鲜，服务也非常到位，强烈推荐！',
            rating: 5, // 评分字段
            replies: [{
                user: {
                    name: '商家'
                },
                date: '2025-06-11',
                content: '感谢您的喜爱！我们会继续努力提供优质的美食和服务~'
            }]
        },
        {
            id: 2,
            user: {
                name: '小吃货'
            },
            date: '2025-06-09',
            content: '味道不错，分量也很足，就是稍微有点咸，可能我口味比较淡吧。环境很温馨，适合朋友小聚。',
            rating: 4, // 评分字段
            
        },
        {
            id: 3,
            user: {
                name: '挑剔食客'
            },
            date: '2025-06-08',
            content: '中规中矩的一家店，没有特别惊艳的地方，但也没有明显缺点。价格适中，适合日常用餐。',
            rating: 3, // 评分字段
            replies: [{
                user: {
                    name: '商家'
                },
                date: '2025-06-09',
                content: '感谢您的评价！我们会不断改进，争取给您带来更惊艳的体验~'
            }]
        }
    ],
    questions: [{
            id: 1,
            user: {
                name: '好奇宝宝'
            },
            date: '2025-06-10',
            question: '这道菜辣吗？不能吃辣的人能接受吗？',
            answers: [{
                    user: {
                        name: '热心顾客'
                    },
                    date: '2025-06-10',
                    answer: '我觉得不算很辣，属于微辣级别，不能吃辣的人稍微注意一下应该可以接受。',
                    isMerchantAnswer: false
                },
                {
                    user: {
                        name: '商家'
                    },
                    date: '2025-06-11',
                    answer: '您好，这道菜我们可以根据您的口味调整辣度，完全不能吃辣的话可以选择不辣的版本，欢迎尝试！',
                    isMerchantAnswer: true
                }
            ]
        },
        {
            id: 2,
            user: {
                name: '美食探索家'
            },
            date: '2025-06-09',
            question: '请问这道菜的分量够两个人吃吗？',
            answers: [{
                user: {
                    name: '商家'
                },
                date: '2025-06-09',
                answer: '您好，这道菜的分量适中，一般来说一个人吃足够，两个人作为分享菜品也可以，建议根据您的食量选择哦~',
                isMerchantAnswer: true
            }]
        },
    ],
    // 表单状态存储
    showingReplyForm: {},
    showingAnswerForm: {},
    replyText: {},
    answerText: {},
    questionText: '',
    // 评分相关状态
    currentRating: 5, // 当前选中的评分
    reviewForm: {
        content: '',
        rating: 5 // 默认评分
    }
};

// 工具函数
const utils = {
    // 日期格式化
    formatDate(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    },
    // 评分转星级HTML
    ratingToStars(rating) {
        let stars = '';
        for (let i = 1; i <= 5; i++) {
            stars += `<span class="star ${i <= rating ? 'filled' : ''}">★</span>`;
        }
        return stars;
    }
};

// 评价功能函数
const reviewFunctions = {
    // 显示回复表单
    showReplyForm(reviewId, updateCallback) {
        data.showingReplyForm[reviewId] = true;
        updateCallback && updateCallback(reviewId);
    },
    // 隐藏回复表单
    hideReplyForm(reviewId, updateCallback) {
        data.showingReplyForm[reviewId] = false;
        data.replyText[reviewId] = '';
        updateCallback && updateCallback(reviewId);
    },
    // 提交回复
    submitReply(reviewId, updateCallback) {
        if (!data.replyText[reviewId]) return;
        const review = data.reviews.find(r => r.id === reviewId);
        if (review) {
            review.replies = review.replies || [];
            review.replies.push({
                user: {
                    name: '商家'
                },
                date: utils.formatDate(new Date()),
                content: data.replyText[reviewId]
            });
            this.hideReplyForm(reviewId, updateCallback);
        }
    },
    // 设置评分
    setRating(rating, updateCallback) {
        data.reviewForm.rating = rating;
        updateCallback && updateCallback(rating);
    },
    // 提交评价
    submitReview(updateCallback) {
        if (!data.reviewForm.content.trim()) return;

        data.reviews.unshift({
            id: data.reviews.length + 1,
            user: {
                name: '商家'
            },
            date: utils.formatDate(new Date()),
            content: data.reviewForm.content,
            rating: data.reviewForm.rating,
            likes: 0,
            likedByCurrentUser: false, // 新增：默认未点赞
            replies: []
        });

        // 重置表单
        data.reviewForm.content = '';
        data.reviewForm.rating = 5;

        updateCallback && updateCallback();
    }
};

// 问答功能函数
const qaFunctions = {
    // 提交问题
    submitQuestion(updateCallback) {
        if (!data.questionText) return;
        data.questions.unshift({
            id: data.questions.length + 1,
            user: {
                name: '商家'
            },
            date: utils.formatDate(new Date()),
            question: data.questionText,
            answers: []
        });
        data.questionText = '';
        updateCallback && updateCallback();
    },
    // 显示回答表单
    showAnswerForm(questionId, updateCallback) {
        data.showingAnswerForm[questionId] = true;
        updateCallback && updateCallback(questionId);
    },
    // 隐藏回答表单
    hideAnswerForm(questionId, updateCallback) {
        data.showingAnswerForm[questionId] = false;
        data.answerText[questionId] = '';
        updateCallback && updateCallback(questionId);
    },
    // 提交回答
    submitAnswer(questionId, updateCallback) {
        if (!data.answerText[questionId]) return;
        const question = data.questions.find(q => q.id === questionId);
        if (question) {
            question.answers = question.answers || [];
            question.answers.push({
                user: {
                    name: '商家'
                },
                date: utils.formatDate(new Date()),
                content: data.answerText[questionId],
                isMerchantAnswer: true
            });
            this.hideAnswerForm(questionId, updateCallback);
        }
    }
};

// 标签页切换功能
const tabFunctions = {
    switchToTab(tabName, updateCallback) {
        data.activeTab = tabName;
        updateCallback && updateCallback(tabName);
    }
};

// 导出整合后的数据与功能
export const reviewData = {
    data,
    utils,
    review: reviewFunctions,
    qa: qaFunctions,
    tabs: tabFunctions
};
